package com.bobby.web;

import com.bobby.pojo.*;
import com.bobby.service.BookService;
import com.bobby.service.OrderService;
import com.bobby.service.impl.BookServiceImpl;
import com.bobby.service.impl.OrderServiceImpl;
import com.bobby.utils.JdbcUtils;
import com.bobby.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2020/8/16 16:30
 * @Created wenjunpei
 */
public class OrderServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();


    protected void checkout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Cart cart = WebUtils.getCart(request);
        User user = (User) request.getSession().getAttribute("user");
        if(user == null) {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
            return;
        }

        String orderId = null;

        orderId = orderService.createOrder(cart, user.getId());

        request.getSession().setAttribute("orderId",orderId);
        response.sendRedirect(request.getContextPath() + "/pages/cart/checkout.jsp");
    }


    protected void showMyOrders(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        if(user == null) {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
            return;
        }
        List<Order> orders = null;

        orders = orderService.showMyOrders(user.getId());

        request.setAttribute("orders",orders);
        request.getRequestDispatcher("/pages/order/order.jsp").forward(request,response);
    }

    protected void showOrderDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String orderId = request.getParameter("orderId");
        List<OrderItem> orderItems = null;

        orderItems = orderService.showOrderDetail(orderId);

        request.setAttribute("orderItems",orderItems);
        request.getRequestDispatcher("/pages/order/order_detail.jsp").forward(request,response);
    }

    protected void showAllOrders(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Order> showAllOrders = null;

        showAllOrders = orderService.showAllOrders();

        request.setAttribute("showAllOrders",showAllOrders);
        request.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(request,response);
    }

    protected void sendOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String orderId = request.getParameter("orderId");

        orderService.sendOrder(orderId);

        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void receiveOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String orderId = request.getParameter("orderId");

        orderService.receiveOrder(orderId);

        response.sendRedirect(request.getHeader("Referer"));
    }
}
